// 路由鉴权
import router from '@/router'
import { close, start } from '@/utils/nprogress'
// 获取token 判断用户是否登录成功
import { useUserStore } from './stores/modules/user'
import setting from './settings'

/**
 * 全局前置守卫
 * @param to 将要访问的路由
 * @param from 从哪个路由来
 * @param next 路由的放行函数
 */
router.beforeEach((to: any, from: any, next: any) => {
  document.title = `${setting.title}-${to.meta.title}`
  start()
  let userStore = useUserStore()
  let token = userStore.token
  if (token) {
    if (to.path == '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    if (to.path == '/login') {
      next()
    } else {
      next({ path: '/login', query: { redirect: to.path } })
    }
  }
})

// 全局后置守卫
router.afterEach((to: any, from: any) => {
  close()
})
